<!doctype html>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../assert_selection.js"></script>
<script>
// TODO(editing-dev): Once http://crbug.com/736253 fixed, we should use
// |chrome.pointerActionSequence()| instead of |eventSender|.

function moveToMiddle(selection, target) {
  assert_own_property(window, 'eventSender', 'This test requires eventSender.');
  eventSender.mouseMoveTo(
      selection.computeLeft(target),
      selection.computeTop(target) + target.offsetHeight / 2);
}

function doDoubleClick(selection, target) {
  moveToMiddle(selection, target);
  eventSender.mouseDown();
  eventSender.mouseUp();
  eventSender.mouseDown();
  eventSender.mouseUp();
}

function doShiftClick(selection, target) {
  const document = selection.document;
  const parent = target.parentNode;
  moveToMiddle(selection, target);
  eventSender.mouseDown(0, ['shiftKey']);
  eventSender.mouseUp(0, ['shiftKey']);
}

function resetMouse() {
  eventSender.mouseMoveTo(0, 0);
  eventSender.mouseDown();
  eventSender.mouseUp();
  eventSender.leapForward(999);
}

function doTest1(behavior) {
  assert_own_property(window, 'internals', 'This test requires internals.');
  internals.settings.setEditingBehavior(behavior);
  internals.settings.setSelectTrailingWhitespaceEnabled(behavior === 'win');

  const kSample1 = [
    '<div>',
      '<span id="one">one </span>',
      '<span id="two">a </span>',
      '<span id="three">three </span>',
    '</div>',
  ].join('');

  assert_selection(
    kSample1,
    selection => {
      const one = selection.document.getElementById('one');
      const two = selection.document.getElementById('two');
      const three = selection.document.getElementById('three');

      resetMouse();
      doDoubleClick(selection, two);
    },
    [
      '<div>',
        '<span id="one">one </span>',
        behavior === 'win'
            ? '<span id="two">^a |</span>'
            : '<span id="two">^a| </span>',
        '<span id="three">three </span>',
      '</div>',
    ].join(''),
    '2-1 double-click');

  assert_selection(
    kSample1,
    selection => {
      const one = selection.document.getElementById('one');
      const two = selection.document.getElementById('two');
      const three = selection.document.getElementById('three');

      resetMouse();
      doDoubleClick(selection, two);
      doShiftClick(selection, one);
    },
    [
      '<div>',
        '<span id="one">|one </span>',
        '<span id="two">a^ </span>',
        '<span id="three">three </span>',
      '</div>',
    ].join(''),
    '2-2 double-click, extend backward');

  assert_selection(
    kSample1,
    selection => {
      const one = selection.document.getElementById('one');
      const two = selection.document.getElementById('two');
      const three = selection.document.getElementById('three');

      resetMouse();
      doDoubleClick(selection, two);
      doShiftClick(selection, one);
      doShiftClick(selection, three);
    },
    behavior === 'mac'
      ? [
          '<div>',
            '<span id="one">^one </span>',
            '<span id="two">a </span>',
            '<span id="three">three| </span>',
          '</div>',
        ].join('')
      : [
          '<div>',
            '<span id="one">one </span>',
            '<span id="two">^a </span>',
            '<span id="three">three| </span>',
          '</div>',
        ].join(''),
    '2-3 double-click, extend backward, extend forward');
}

function doTest2(behavior) {
  assert_own_property(window, 'internals', 'This test requires internals.');
  internals.settings.setEditingBehavior(behavior);

  const kSample2 = [
    '<div>',
      '<span id="one">one </span>',
      '<span id="two">two </span>',
      '<span id="three">three </span>',
    '</div>',
  ].join('');

  assert_selection(
    kSample2,
    selection => {
      const one = selection.document.getElementById('one');
      const two = selection.document.getElementById('two');
      const three = selection.document.getElementById('three');

      resetMouse();
      doDoubleClick(selection, two);
    },
    [
      '<div>',
        '<span id="one">one </span>',
        behavior === 'win'
            ? '<span id="two">^two |</span>'
            : '<span id="two">^two| </span>',
        '<span id="three">three </span>',
      '</div>',
    ].join(''),
    '2-1 double-click');

  assert_selection(
    kSample2,
    selection => {
      const one = selection.document.getElementById('one');
      const two = selection.document.getElementById('two');
      const three = selection.document.getElementById('three');

      resetMouse();
      doDoubleClick(selection, two);
      doShiftClick(selection, one);
    },
    [
      '<div>',
        '<span id="one">|one </span>',
        '<span id="two">two^ </span>',
        '<span id="three">three </span>',
      '</div>',
    ].join(''),
    '2-2 double-click, extend backward');

  assert_selection(
    kSample2,
    selection => {
      const one = selection.document.getElementById('one');
      const two = selection.document.getElementById('two');
      const three = selection.document.getElementById('three');

      resetMouse();
      doDoubleClick(selection, two);
      doShiftClick(selection, one);
      doShiftClick(selection, three);
    },
    behavior === 'mac'
      ? [
        '<div>',
          '<span id="one">^one </span>',
          '<span id="two">two </span>',
          '<span id="three">three| </span>',
        '</div>',
      ].join('')
      : [
        '<div>',
          '<span id="one">one </span>',
          '<span id="two">^two </span>',
          '<span id="three">three| </span>',
        '</div>',
      ].join(''),
    '2-3 double-click, extend backward, extend forward');
}

for (const behavior of ['android', 'mac', 'unix','win']) {
  test(() => doTest1(behavior),
       `${behavior}: 1 Extend by word backward then forward:`);
  test(() => doTest2(behavior),
       `${behavior}: 2 Extend by word backward then forward:`);
}
</script>
